package com.jhd.fuzhou.view.document;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.service.PDFService;
import com.jhd.fuzhou.util.UtilHelper;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.vo.DocTemplateVo;
import com.jhd.fuzhou.vo.JsonForResult;

import net.zigin.commons.util.ParamsMapUtil;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class DocumentManageActivity extends BaseActivity {

    private List<DocTemplateVo> docTemplateVoList;
    private Context context=this;
    @BindView(R.id.recyclerView)RecyclerView recyclerView;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_document_manage);

    }

    @Override
    protected void processLogic() {
        onBack();
        getDocTemplateList();//获取文书模板
       // application.addDocActivity(this);
        LinearLayoutManager manager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(manager);
    }

    /*获取文书模板*/
    public void getDocTemplateList(){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        PDFService vs = retrofit.create(PDFService.class);
        Map<String, String> params = ParamsMapUtil.getInstance("docType","指导性文书").addParam("deptName",application.getDeptName());
        Call<JsonForResult> jsonForResultCall = vs.getDocTemplateList(application.getAccessToken(),params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                JsonForResult jsonForResult = (JsonForResult) response.body();
                if (jsonForResult != null && jsonForResult.isSuccess()) {
                    //请求授权信息
                    docTemplateVoList= JSON.parseArray(jsonForResult.getData().toString(), DocTemplateVo.class);
                    if(docTemplateVoList!=null){
                        DocAdapter adapter=new DocAdapter(docTemplateVoList);
                        recyclerView.setAdapter(adapter);
                    }
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                String msg = "获取文书模板失败!";
                System.out.println(t.getStackTrace());
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }

    public class DocAdapter extends RecyclerView.Adapter<DocAdapter.ViewHolder> {
        List<DocTemplateVo> docTemplateVoList;
        public DocAdapter(List<DocTemplateVo> docTemplateVoList) {
            this.docTemplateVoList = docTemplateVoList;
        }

        @NonNull
        @Override
        public DocAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_document_manage, parent, false);
            DocAdapter.ViewHolder viewHolder = new DocAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull DocAdapter.ViewHolder holder, int position) {
            holder.btn_name.setText(docTemplateVoList.get(position).getName()==null?  docTemplateVoList.get(position).getFileName() :docTemplateVoList.get(position).getName());
            holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context,DocumentPrintActivity.class);
                    intent.putExtra("url",docTemplateVoList.get(position).getFileUrl());
                    intent.putExtra("name",docTemplateVoList.get(position).getName()==null?  docTemplateVoList.get(position).getFileName() :docTemplateVoList.get(position).getName());
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return docTemplateVoList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView btn_name;
            LinearLayout linearLayout;
            public ViewHolder(View itemView) {
                super(itemView);
                this.btn_name = itemView.findViewById(R.id.btn_name);
                this.linearLayout = itemView.findViewById(R.id.linearLayout);
            }
        }
    }
}